package com.jinbiao.jdbc;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

public class Student_Template implements StudentDao  {

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate;
	
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource=dataSource;
		this.jdbcTemplate= new JdbcTemplate(dataSource);
	}

	public void create(String name, Integer age) {
		String sql = "insert into Student (name,age) values (?,?)";
		jdbcTemplate.update(sql, name,age);
		System.out.println("添加记录"+"name="+name+"age="+age);
		
	}

	public Student getStudent(Integer id) {
	
		String SQL = "select * from Student where id = ?";
	      Student student = jdbcTemplate.queryForObject(SQL, 
	                        new Object[]{id}, new StudentMapper());
	      return student;
	}

	public List<Student> listStudents() {
		String sql =  "select * from student";
		List<Student> list =  jdbcTemplate.query(sql, new StudentMapper());
		return list;
	}

	/**
	 * 根据id 进行删除
	 */
	public void delete(Integer id) {
		
		String sql = "delete from student where id =?";
		jdbcTemplate.update(sql,new Object[] {id});
		System.out.println("学生为id="+id+"删除成功");
	}

	
	public void update(Integer id, Integer age) {
		String sql =  "update student set age = ? where id =?";
		jdbcTemplate.update(sql, new Object[] {age,id});
		System.out.println("学生id="+id+"年龄更新成功");
	}

}
